package com.example.flowermall.controller;


import com.example.flowermall.config.VerifyCode;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.example.flowermall.domain.LoginBody;
import com.example.flowermall.domain.LoginUser;
import com.example.flowermall.entity.Menu;
import com.example.flowermall.entity.Result;
import com.example.flowermall.entity.User;
import com.example.flowermall.service.ILoginService;
import com.example.flowermall.service.IMenuService;
import com.example.flowermall.service.IUserService;
import com.example.flowermall.util.RedisUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:8888")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @Autowired
    IUserService userService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    IMenuService menuService;
    private String codes = "0123456789abcdefghijklmnopqrstuvwxyz";
    @PostMapping("/login")
    public Result login(@RequestBody LoginBody loginBody){
        String  token = loginService.login(loginBody);
        if(ObjectUtils.isEmpty(token)){
            return Result.error("验证码错误");
        }
        Result result = Result.success();
        Map<String ,Object> data=new HashMap<>();
        data.put("token",token);
        result.put("data",data);
        return  result;
    }

    @PostMapping("/wxLogin")
    public Result wxLogin(@RequestBody LoginBody loginBody){
        String  token = loginService.wxlogin(loginBody);
        if(ObjectUtils.isEmpty(token)){
            return Result.error("验证码错误");
        }
        Result result = Result.success();
        Map<String ,Object> data=new HashMap<>();
        data.put("token",token);
        result.put("data",data);
        return  result;
    }
    @GetMapping("/info")
    public Result info(){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = loginUser.getUser();
        Result result = Result.success();
        Map<String ,Object> data=new HashMap<>();
        data.put("username",user.getUsername());
        data.put("user",user);
        data.put("userId",user.getId());
        data.put("avatar",user.getAvatar());
        List<String> roleName = loginService.getRoleNameByUserId(user.getId());
        data.put("roleName",roleName);
        List<Menu> menuList = menuService.getMenuListByUserList(user.getId());
        data.put("menuList",menuList);
        User serviceUser = userService.getUser(user.getId());
        List<User> beCode = userService.getBeCode(serviceUser.getInvitationCode());
        data.put("beInvitedNum",beCode.size());
        if(ObjectUtils.isEmpty(beCode)){
            data.put("beInvitedNum",0);
        }
        data.put("rebate",serviceUser.getRebate());
        result.put("data",data);
        return result;
    }
    @PostMapping("/logout")
    public Result logout(@RequestHeader(value = "X-Token",required = false)String token){
        loginService.logout(token);
        return Result.success("退出成功");
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if(ObjectUtils.isEmpty(user.getBeInvitedCode())){
            return Result.error("邀请码不能为空");
        }
        if(ObjectUtils.isEmpty(user.getPhone())){
            return Result.error("电话号码不能为空");
        }
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return userService.register(user);
    }

    /*@GetMapping("/wxlogin")
    public Result getSessionId(@RequestParam("code") String code,
                               @RequestParam("encryptedData")String encryptedData,
                               @RequestParam("iv")String iv) throws Exception {
        String token = loginService.getSessionId(code, encryptedData, iv);
        if(token==null) {
            return Result.error("生成token失败");
        }else {
            Result success = Result.success();
            success.put("token", token);
            return  success;
        }
    }*/

    @PostMapping("/wxLogout")
    public Result wxLogout(@RequestBody String token){
        loginService.logout(token);
        return Result.success("退出成功");
    }



}
